#Alexander F. Gazmararian
#afg2@princeton.edu
#January 19, 2024

set.seed(10)
# Load packages and data
library(tidyverse)
library(stm)
library(here)

#Load data
g <- readRDS(here("data", "qualtrics", "natsurvey.rds"))

# Prepare data
proc <- textProcessor(g$ProfitOE, metadata = g)
out <- prepDocuments(proc$documents, proc$vocab, proc$meta)
docs <- out$documents
vocab <- out$vocab
meta <- out$meta

# Estimate structural topic model
profit.stm <- stm(documents = docs, vocab = vocab, K = 10)

#Create Figure E1: stm results for topics raised in the open-ended question about
# donation allocation. Installation costs are the most frequently mentioned topic
png(filename = here("output", "figures", "fig_stm_openallocate.png"), width = 6.5, height = 6.5, units = "in", res=300)
plot(profit.stm, topic.names = paste0(c(
  "Debt and Other Expenses",
  "Gifts",
  "Donate",
  "Save on Electric Bill",
  "Pay for Bills",
  "Savings",
  "Save and Invest",
  "Installation Costs",
  "Home Improvement",
  "Household Repairs"),
  ":"),
  n = 5
)
dev.off()
